﻿using System;
using System.Data;
using System.Threading;
using System.Windows.Forms;
using FWPDocumentation.Common.DbTools;
using FWPDocumentation.Entities;
using MySql.Data.MySqlClient;

namespace FWPDocumentation.VisualForms
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			UpdateDatagrid();
		}

		private void OnFormLoad(object sender, EventArgs e)
		{
			DbIface.Connection.Open();

		}

		private void Button1Click(object sender, EventArgs e)
		{
			FormAddStudent fadd = new FormAddStudent(dataGridView1);
            fadd.Show();
		}

		private void Button2Click(object sender, EventArgs e)
		{
			var t = new Thread(ThreadProc2);
			t.Start();
		}

		private void OnFormClosed(object sender, FormClosedEventArgs e)
		{
			DbIface.Connection.Close();
		}

		public void UpdateDatagrid()
		{
			dataGridView1.Columns.Clear();
			dataGridView1.Refresh();
			var ds = new DataSet();
			var da = new MySqlDataAdapter(new MySqlCommand(
                "SELECT Institute as Институт, Faculty as Факультет, `group` as Группа, Surname as Фамилия, Name as Имя, MiddleName as Отчество,  TroopNumber as Взвод, GPA as `Средний бал`, " +
                "TEST as `Тест`, MedicalCommission as `Мед. комиссия`, ProfSelection as `Проф. отбор`, PhysSelection as `ФизО`, Payment as `Оплата`, " +
                "AdmissionYear as `Год поступления` FROM maininfo,institute, faculty, `group`, admission, Troop WHERE maininfo.Group_idGroup = `group`.idGroup " +
                "AND `group`.Faculty_idFaculty = faculty.idFaculty AND faculty.Institute_idInstitute = institute.idInstitute " +
                "AND maininfo.idMainInfo = admission.MainInfo_idMainInfo;", DbIface.Connection));
			da.Fill(ds, "maininfo");
			DataView view = ds.Tables["maininfo"].AsDataView();
			dataGridView1.DataSource = view;
		}

		#region Служебные методы

		private static void ThreadProc()
		{
		    Application.Run(new FormAddStudent());
		}

		private static void ThreadProc2()
		{
			Application.Run(new Form3());
		}

		#endregion

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


	}
}
